//
//  LLGoodsDetailViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/6.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLGoodsDetailViewController.h"
#import "LLShopGoodsModel.h"
#import "LLTransparentNavView.h"
#import "LLGoodsDetailHeaderView.h"
#import "LLMallDoCell.h"
#import "LLMallDoModel.h"
#import "LLMallDoSectionHeader.h"
#import "LLMallHeaderView.h"
#import "LLGoodsDetailBottomView.h"
#import "LLOrderView.h"
#import "LLClusterListPopView.h"
#import "LLClusterPopView.h"
#import "LLMallDoSuccessViewController.h"
#import "LLShoppingCartViewController.h"


@interface LLGoodsDetailViewController ()<LLTransparentNavViewDelegate, LLMallDoSectionHeaderDelegate, LLGoodsDetailBottomViewDelegate, LLMallDoCellDelegate, LLClusterListPopViewDelegate, LLClusterPopViewDelegate>
@property (strong, nonatomic) LLGoodsDetailHeaderView *headerView;
@property (assign, nonatomic) CGFloat tableHeaderViewH;
@property (strong, nonatomic) LLMallDoSectionHeader *sectionHeader;
@property (strong, nonatomic) LLOrderView *orderView;
@property (strong, nonatomic) LLClusterListPopView *clusterListPopView;
@property (strong, nonatomic) LLClusterPopView *clusterPopView;
@end

@implementation LLGoodsDetailViewController

- (void)viewDidLoad {
    self.notConfigTableViewRightAway = YES;
    [super viewDidLoad];
    [self initData];
    self.tableViewStyle = UITableViewStyleGrouped;
    [self configTableView];
    [self initNavigationBar];
    [self initBottomView];
    [self loadNetworkData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)initData {
    CGFloat titleH = [CommonTools sizeOfText:self.model.goods_name fontSize:15 width:LLScreenWidth-26].height;
    self.tableHeaderViewH = LLScreenWidth*315/375+10+20+10+titleH+10+10+40+10;
    self.model.goods_classify_name = @"已拼1万件";
    self.model.goods_name = @"7节课程 · 随时开课";
}

- (void)loadNetworkData {
    for (NSInteger i = 0; i < 10; i++) {
        LLMallDoModel *model = LLMallDoModel.new;
        model.imgUrl = @"";
        model.nickname = @"Honon";
        model.countStr = @"2";
        model.remainingTime = @"12:34:00";
        model.doText = @"去拼单";
        [self.models addObject:model];
    }
    [self.tableView reloadData];
}

- (void)initNavigationBar {
    LLTransparentNavView *navView = [[LLTransparentNavView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, self.navH)];
    navView.leftItemImg = @"back_black";
    navView.rightItemImg = @"share_black";
    navView.delegate = self;
    [self.view addSubview:navView];
}

- (void)initBottomView {
    LLGoodsDetailBottomView *bottomView = [[LLGoodsDetailBottomView alloc] initWithFrame:CGRectMake(0, LLScreenHeight-70, LLScreenWidth, 70) type:LLGoodsDetailBottomViewTypeSwitch];
    bottomView.delegate = self;
    bottomView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:bottomView];
}

- (void)configTableView {
    [super configTableView];
    self.tableView.top = 0;
    self.tableView.height = LLScreenHeight-70;
    self.tableView.rowHeight = 60;
    [self.tableView registerCellByClassName:@"LLMallDoCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorInset = UIEdgeInsetsZero;
    
    LLGoodsDetailHeaderView *headerView = [[LLGoodsDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, self.tableHeaderViewH)];
    headerView.backgroundColor = UIColor.whiteColor;
    headerView.model = self.model;
    self.tableView.tableHeaderView = headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.sectionHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LLMallDoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLMallDoCell"];
    cell.type = LLMallDoCellTypeOther;
    cell.delegate = self;
    cell.model = self.models[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - LLMallDoCellDelegate

- (void)cellDidClickDo:(LLMallDoCell *)cell {
    
}

#pragma mark - LLTransparentNavViewDelegate

- (void)transparentNavViewDidClickItemWithIndex:(NSInteger)index {
    if (index == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - LLMallDoSectionHeaderDelegate

- (void)sectionHeaderDidClickMore {
    [self.clusterListPopView show];
}

#pragma mark - LLGoodsDetailBottomViewDelegate

- (void)goodsDetailBottomViewDidClickButtonWithIndex:(NSInteger)index {
    if (index == 1) {
        [self.navigationController pushViewController:LLShoppingCartViewController.new animated:YES];
    }
    
//    [self.orderView show];
}

#pragma mark - LLClusterListPopViewDelegate

- (void)clusterListPopViewDidClickCell:(LLMallDoCell *)cell {
    [self.clusterListPopView hide];
    self.clusterPopView.model = cell.model;
    self.clusterPopView.imgs = @[@"",@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.clusterPopView show];
    });
}

#pragma mark - LLClusterPopViewDelegate

- (void)clusterPopViewDidClickCluster {
    [self.clusterPopView hide];
    LLMallDoSuccessViewController *vc = LLMallDoSuccessViewController.new;
    vc.type = LLMallDoSuccessViewControllerTypeCluster;
    vc.price = @"23.5";
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - lazy load

- (LLMallDoSectionHeader *)sectionHeader {
    if (!_sectionHeader) {
        _sectionHeader = [[LLMallDoSectionHeader alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, 44)];
        _sectionHeader.backgroundColor = UIColor.whiteColor;
        _sectionHeader.delegate = self;
        _sectionHeader.title = @"10人正在拼团";
    }
    return _sectionHeader;
}

- (LLOrderView *)orderView {
    if (!_orderView) {
        _orderView = [[LLOrderView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight)];
        _orderView.model = self.model;
        [self.view addSubview:_orderView];
    }
    return _orderView;
}

- (LLClusterListPopView *)clusterListPopView {
    if (!_clusterListPopView) {
        _clusterListPopView = [[LLClusterListPopView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight)];
        _clusterListPopView.delegate = self;
        _clusterListPopView.models = self.models;
        _clusterListPopView.title = @"正在拼团";
        [self.view addSubview:_clusterListPopView];
    }
    return _clusterListPopView;
}

- (LLClusterPopView *)clusterPopView {
    if (!_clusterPopView) {
        _clusterPopView = [[LLClusterPopView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight)];
        _clusterPopView.clusterTitle = @"参与拼团";
        _clusterPopView.delegate = self;
        [self.view addSubview:_clusterPopView];
    }
    return _clusterPopView;
}

@end
